package com.example.dbsample.util.datasource;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.example.dbsample.util.dynamicconnection.DynamicEsSourceContextHolder;
import com.example.dbsample.util.dynamicconnection.DynamicRedisSourceContextHolder;
import com.example.dbsample.util.spring.AppIdContextHolderUtil;

public class DsUtils {
    /**
     * 设置数据源
     */
    public static void SetDs(String appId) {
        DynamicDataSourceContextHolder.push(appId);
        DynamicRedisSourceContextHolder.set(appId);
        DynamicEsSourceContextHolder.set(appId);
    }

    public static void setAppId(String appId) {
        AppIdContextHolderUtil.set(appId);
    }

    public static void setDsAndAppId(String appId) {
        DynamicDataSourceContextHolder.push(appId);
        DynamicRedisSourceContextHolder.set(appId);
        DynamicEsSourceContextHolder.set(appId);
        AppIdContextHolderUtil.set(appId);
    }

    /**
     * 消除数据源，防止内存泄漏
     */
    public static void clearDsAndAppId() {
        DynamicDataSourceContextHolder.clear();
        DynamicRedisSourceContextHolder.clear();
        DynamicEsSourceContextHolder.clear();
        AppIdContextHolderUtil.clear();
    }
}
